<template>
  <div class="login-account">
    <el-form label-width="60px" :rules="rules" :model="account" ref="formRef">
      <el-form-item label="账号" prop="username">
        <el-input v-model="account.username" />
      </el-form-item>
      <el-form-item label="密码" prop="password">
        <el-input v-model="account.password" show-password />
      </el-form-item>
    </el-form>
  </div>
</template>

<script lang="ts">
import { defineComponent, reactive, ref } from "vue"
import { ElForm } from "element-plus"
import Storage from "@/utils/storage"
import { loginStore } from "@/stores/modules/login"

import { rules } from "../config/account-config"
// import Requests from "@/service"

export default defineComponent({
  setup() {
    const account = reactive({
      username: Storage.getStorage("username") ?? "",
      password: Storage.getStorage("password") ?? ""
    })

    const formRef = ref<InstanceType<typeof ElForm>>()

    const LS = loginStore()

    // 登录
    const loginAction = (isKeepPassword: boolean) => {
      formRef.value?.validate(valid => {
        if (valid) {
          // 1.判断是否需要记住密码
          if (isKeepPassword) {
            // 本地缓存
            Storage.setStorage("username", account.username)
            Storage.setStorage("password", account.password)
          } else {
            Storage.deleteStorage("username")
            Storage.deleteStorage("password")
          }
          // 2.登录
          LS.Login({ ...account })
        }
      })
    }

    return {
      account,
      rules,
      loginAction,
      formRef
    }
  }
})
</script>

<style scoped></style>
